package com.dt76.smallloanfrontenduser.controller.etao;

import com.dt76.smallloanfrontenduser.pojo.etao.User;
import com.dt76.smallloanfrontenduser.remote.etao.UserRemote;
import com.dt76.smallloanfrontenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserRemote userRemote;

    @Autowired
    private JavaMailSenderImpl mailSender;


    //获取动态验证码,并发送邮件
    @RequestMapping("/getCode")
    @ResponseBody
    public String getCode(String email) {
        String code = (int) ((Math.random() * 9 + 1) * 100000) + "";
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("913753579@qq.com");
        message.setTo(email);
        message.setSubject("邮箱登录验证");
        message.setText(code);
        mailSender.send(message);
        System.out.println(code);
        return code;
    }

    //进行邮箱动态登录的验证
    @RequestMapping("/doLoginByEmail")
    @ResponseBody
    public Result doLoginByEmail(String email, HttpSession session) {
        Result result = userRemote.doLoginByEmail(email);
        System.out.println(result.isSuccess());
        if (result.isSuccess()) {
            result.setMsg("登录成功");
            User currentUser = result.getCurrentUser();
            session.setAttribute("currentUser", currentUser);
            result.setUrl("/main?userId="+currentUser.getUserId());
        } else {
            result.setMsg("此邮箱未注册,请先注册");
        }
        return result;
    }


    //普通用户名登录
    @RequestMapping("/doLogin")
    @ResponseBody
    public Result doLogin(String userName, String password, HttpSession session) {
        //进行用户名和密码验证
        Result result = userRemote.doLogin(userName, password);
        System.out.println(result.isSuccess());
        if (result.isSuccess()) {
            result.setMsg("登录成功");
            User currentUser = result.getCurrentUser();
            //当前登录人保存在session中
            session.setAttribute("currentUser",currentUser);
            result.setUrl("/main?userId="+currentUser.getUserId());
        } else {
            result.setMsg("用户名或密码错误");
            //验证失败,回到登录页面
            result.setUrl("/admin");
        }
        return result;
    }

    //注册账号
    @RequestMapping("/register")
    public String doDegister(User user, Model model) {
        //取到页面传递的参数,添加新账户
        Result result = userRemote.insertUser(user);
        if (result.isSuccess()) {
            //注册成功后,跳转到admin登录页面
            //将注册的登录名返回到登录页面
            model.addAttribute("newLoginName", user.getLoginName());
            return "admin";
        }
        return "regist";
    }



}
